package com.apexcode.friend.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.apexcode.common.core.domain.TableDataInfo;
import com.apexcode.friend.domain.question.Question;
import com.apexcode.friend.domain.question.dto.QuestionQueryDTO;
import com.apexcode.friend.domain.question.es.QuestionES;
import com.apexcode.friend.domain.question.vo.QuestionVO;
import com.apexcode.friend.domain.user.vo.QuestionDetailVO;
import com.apexcode.friend.elasticsearch.QuestionESRepository;
import com.apexcode.friend.manager.QuestionCacheManager;
import com.apexcode.friend.mapper.question.QuestionMapper;
import com.apexcode.friend.service.question.IQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName QuestionServiceImpl
 * @Description
 * @Author ZJX
 * @Date 2025/3/24 13:18
 * @Version 1.0
 **/
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionESRepository questionESRepository;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionCacheManager questionCacheManager;

    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
//        先从es查 在查数据库
        long count = questionESRepository.count();
        if (count <= 0) {
            refreshQuestion();
        }
        Sort createTime = Sort.by(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(questionQueryDTO.getPageNum() - 1, questionQueryDTO.getPageSize(), createTime);
        Integer difficulty = questionQueryDTO.getDifficulty();
        String keyword = questionQueryDTO.getKeyword();
        Page<QuestionES> questionESPage = null;
        if (difficulty == null && StrUtil.isEmpty(keyword)){
            questionESRepository.findAll(pageable);
        } else if (StrUtil.isEmpty(keyword)){
            questionESPage = questionESRepository.findQuestionByDifficulty(difficulty, pageable);
        } else if (difficulty == null){
            questionESPage = questionESRepository.findByTitleOrContent(keyword, keyword, pageable);
        } else {
            questionESPage = questionESRepository.findByTitleOrContentAndDifficulty(keyword, keyword, difficulty, pageable);
        }
        long totalElements = questionESPage.getTotalElements();
        if (totalElements <= 0){
            return TableDataInfo.empty();
        }

        List<QuestionES> contentList = questionESPage.getContent();
        List<QuestionVO> questionVOList = BeanUtil.copyToList(contentList, QuestionVO.class);
        return TableDataInfo.success(questionVOList, totalElements);
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
//        先从es查 在查数据库
        QuestionES questionES = questionESRepository.findById(questionId).orElse(null);
        if (questionES != null){
            return BeanUtil.copyProperties(questionES, QuestionDetailVO.class);
        }
        Question question = questionMapper.selectById(questionId);
        if (question == null){
            return null;
        }

        refreshQuestion();
        return BeanUtil.copyProperties(question, QuestionDetailVO.class);
    }

    @Override
    public String preQuestion(Long questionId) {
        Long listSize = questionCacheManager.getListSize();
        if (listSize == null || listSize <= 0) {
            questionCacheManager.refreshCache();
        }
        return questionCacheManager.preQuestion(questionId).toString();
    }

    @Override
    public String nextQuestion(Long questionId) {
        return "";
    }

    private void refreshQuestion() {
        List<Question> questions = questionMapper.selectList(new LambdaQueryWrapper<Question>());
        if (CollectionUtil.isEmpty(questions)){
            return;
        }

        List<QuestionES> questionES = BeanUtil.copyToList(questions, QuestionES.class);
        questionESRepository.saveAll(questionES);
    }
}
